import requests
import datetime
from MySpider import mydb
import  pymysql

# 此脚本用来将豆包维护的vika表存到本地。
# 其他需要分析vika表并保存至本地数据库的可以参考此脚本。
# Vika 表的公开访问网址
# 豆包维护的三张表Update，Official，Temporary
vika_doubao_Update_url=    'https://vika.cn/nest/v1/shares/shrFaQ1pkHnNrHsj6nEkW/datasheets/dstEBHUH5HWvEweF0q/dataPack'
vika_doubao_Official_url=  'https://vika.cn/nest/v1/shares/shrFaQ1pkHnNrHsj6nEkW/datasheets/dstVAi0UjJnAuoG3rq/dataPack'
vika_doubao_Temporary_url= 'https://vika.cn/nest/v1/shares/shrFaQ1pkHnNrHsj6nEkW/datasheets/dst58UC6lXJKFqNxje/dataPack'

def fetch_vika_doubao_data(vika_url):
    """
    读取 Vika 表数据
    :param vika_url: 豆包的vika表地址
    :return:
    """
    response = requests.get(vika_url)
    if response.status_code == 200:
        return response.json()['data'] # 返回需求数据部分
    else:
        raise Exception(f"Error fetching data from Vika: {response.status_code} {response.text}")

def save_douban_data_2_db(vika_url):
    """
    豆包表格数据存到数据库中
    :param vika_url:
    :return:
    """
    vika_data = fetch_vika_doubao_data(vika_url)
    vika_field_data = vika_data['snapshot']['meta']['fieldMap']
    vika_record_data = vika_data['snapshot']['recordMap']
    # 获取列名对应的一个唯一码
    fields=dict()
    for value in list(vika_field_data.values()):
        fields[value['name']]=value['id']

    for value in vika_record_data.items():
        try:
            createdAt = datetime.datetime.fromtimestamp(value[1]['createdAt']/1000)
            the_id = value[1]['data'][fields['id']][0]['text']
            title =  value[1]['data'][fields['title']][0]['text']
            print(the_id +' '+title+' '+createdAt.strftime('%Y-%m-%d %H:%M:%S'))
            mydb.insert_data_into_table('vika_douban',{'id':the_id,'title':title,'createdAt':createdAt})
        except pymysql.err.IntegrityError as e:
            print("主键重复错误:", e)
        except Exception as e:
            print("捕获到其他错误:", e)

# 保存主表
save_douban_data_2_db(vika_doubao_Official_url)
# 保存临时表
save_douban_data_2_db(vika_doubao_Temporary_url)
